﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
public partial class ss_product : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadproduct();
            loadoriginid();
            loadcateid();
            loadmid();
        }
    }

    private void loadmid()
    {

        SqlDataAdapter da = new SqlDataAdapter("select * from manufacturer", DataAccess.ConnectionString);
        DataSet ds = new DataSet();
        da.Fill(ds, "manufacturer");
        ddlmid.DataSource = ds.Tables["manufacturer"];
        ddlmid.DataTextField = "MName";
        ddlmid.DataValueField = "MId";
        ddlmid.DataBind();
    }

    private void loadcateid()
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from category", DataAccess.ConnectionString);
        DataSet ds = new DataSet();
        da.Fill(ds, "Category");
        ddlcateid.DataSource = ds.Tables["Category"];
        ddlcateid.DataTextField = "Name";
        ddlcateid.DataValueField = "Id";
        ddlcateid.DataBind();
    }

    private void loadproduct()
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from product", DataAccess.ConnectionString);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }

    private void loadoriginid()
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from origin", DataAccess.ConnectionString);
        DataSet ds = new DataSet();
        da.Fill(ds, "Origin");
        ddloriginid.DataSource = ds.Tables["Origin"];
        ddloriginid.DataTextField = "OriginName";
        ddloriginid.DataValueField = "OriginId";
        ddloriginid.DataBind();

    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        try
        {
            SqlCommand cmd = DataAccess.Connection.CreateCommand();
            cmd.CommandText = "insert into product values(@pid,@pname,@size,@color,@price,@describe,@cateid,@originid,@mid,@lang,@description,@keyword,@tang,@picture,@status,@active,@check1,@check2,@check3,@check4,@check5)";
            string file = fileuplaod1.FileName;
            if (fileuplaod1.HasFile)
            {
                if (file.EndsWith("jpg") || file.EndsWith("png") || file.EndsWith("gif"))
                {
                    fileuplaod1.SaveAs(Server.MapPath("~/images/" + file));
                }
                else
                    lblerror.Text="file ko hop le";
            }
            else
                lblerror.Text="ko co file nao dc chon";
            cmd.Parameters.AddWithValue("@pid", txtpid.Text);
            cmd.Parameters.AddWithValue("@pname", txtname.Text);
            cmd.Parameters.AddWithValue("@size", txtsize.Text);
            cmd.Parameters.AddWithValue("@color", txtcolor.Text);
            cmd.Parameters.AddWithValue("@price", txtprice.Text);
            cmd.Parameters.AddWithValue("@describe",CKEditorControl1.Text );
            cmd.Parameters.AddWithValue("@cateid", ddlcateid.SelectedItem.Value);
            cmd.Parameters.AddWithValue("@originid", ddloriginid.SelectedItem.Value);
            cmd.Parameters.AddWithValue("@mid", ddlmid.SelectedItem.Value);
            cmd.Parameters.AddWithValue("@lang", ddllang.SelectedItem.Value);
            cmd.Parameters.AddWithValue("@description", txtdescription.Text);
            cmd.Parameters.AddWithValue("keyword", txtkeyword.Text);
            cmd.Parameters.AddWithValue("@tang",txttang.Text);
            cmd.Parameters.AddWithValue("@picture",file);
            cmd.Parameters.AddWithValue("@status", txtstatus.Text);
            cmd.Parameters.AddWithValue("@active", chkactive.Checked?1:0);
            cmd.Parameters.AddWithValue("@check1", CheckBox1.Checked ? 1 : 0);
            cmd.Parameters.AddWithValue("@check2", CheckBox2.Checked ? 1 : 0);
            cmd.Parameters.AddWithValue("@check3", CheckBox3.Checked ? 1 : 0);
            cmd.Parameters.AddWithValue("@check4", CheckBox4.Checked ? 1 : 0);
            cmd.Parameters.AddWithValue("@check5", CheckBox5.Checked ? 1 : 0);
            cmd.ExecuteNonQuery();
            //
            loadproduct();

        }
        catch (Exception ex)
        {
            lblerror.Text = ex.Message;
        }
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        txtpid.Text = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
        ddlcateid.SelectedValue = GridView1.Rows[e.NewSelectedIndex].Cells[7].Text;
        //ddlcateid.SelectedValue = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
        SqlCommand cmd = DataAccess.Connection.CreateCommand();
        cmd.CommandText = "select * from product where pid=@pid and cateid=@cateid";
        cmd.Parameters.AddWithValue("@pid", txtpid.Text);
        cmd.Parameters.AddWithValue("@cateid", ddlcateid.SelectedValue);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            if (dr.Read())
            {
                txtname.Text = dr.GetString(1);
                txtsize.Text = dr.GetString(2);
                txtcolor.Text = dr.GetString(3);
                txtprice.Text = dr.GetDouble(4).ToString();
                CKEditorControl1.Text = dr.GetString(5);
                //ddlcateid.SelectedValue = dr.GetInt32(6).ToString();
                
            }
        }
    }
}
